import 'package:flame/game.dart';
import 'package:flame_audio/flame_audio.dart';
import 'package:flutter/material.dart';

import 'GameWrapper.dart';
import 'main.dart';

class PauseOverlay extends StatelessWidget {
  final Game game;

  const PauseOverlay(this.game, {super.key});
  
  static const buttonStyle = ButtonStyle(
    backgroundColor: MaterialStatePropertyAll<Color>(Color(0xFF6C91BA)),
    padding: MaterialStatePropertyAll<EdgeInsetsGeometry>(
      EdgeInsets.symmetric(vertical: 16.0),
    ),
    shape: MaterialStatePropertyAll<RoundedRectangleBorder>(
      RoundedRectangleBorder(
        borderRadius: BorderRadius.all(Radius.circular(8.0)),
      ),
    ),
  );

  Widget _buildMenuButton({required VoidCallback? onPressed, required String text}) {
    return SizedBox(
      width: double.infinity,
      child: ElevatedButton(
        style: buttonStyle,
        onPressed: onPressed,
        child: Text(
          text,
          style: const TextStyle(fontSize: 16),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    // 获取GameWrapper组件以访问游戏状态
    GameWrapper? gameWrapper;
    MyGame? myGame;
    if (game is FlameGame) {
      final flameGame = game as FlameGame;
      gameWrapper = flameGame.children.whereType<GameWrapper>().firstOrNull;
      myGame = flameGame as MyGame?;
    }

    return Material(
      color: Colors.black54,
      child: Center(
        child: Container(
          padding: const EdgeInsets.all(24),
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.circular(12),
            boxShadow: const [
              BoxShadow(
                color: Colors.black26,
                blurRadius: 10,
                offset: Offset(0, 4),
              ),
            ],
          ),
          width: 320,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              const Text(
                '游戏暂停',
                style: TextStyle(
                  fontSize: 28,
                  fontWeight: FontWeight.bold,
                  color: Color(0xFF3A6EA5),
                ),
              ),
              const SizedBox(height: 24),
              const Text(
                '游戏已暂停，请选择操作',
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: 16,
                  color: Colors.grey,
                ),
              ),
              const SizedBox(height: 30),
              _buildMenuButton(
                onPressed: () {
                  // 继续游戏
                  if (gameWrapper != null) {
                    gameWrapper.togglePause(); // 调用GameWrapper的公共togglePause方法来同步状态
                  } else {
                    game.overlays.remove('pause'); // 备用方案
                  }
                },
                text: '继续游戏',
              ),
              const SizedBox(height: 12),
              _buildMenuButton(
                onPressed: () {
                  // 重新开始当前关卡
                  if (gameWrapper != null && myGame != null) {
                    gameWrapper.resetScore();
                    gameWrapper.resetTimer();
                    // 重新初始化游戏板
                    myGame.restartGame();
                    FlameAudio.bgm.resume();
                  }
                  game.overlays.remove('pause');
                },
                text: '重新开始',
              ),
              const SizedBox(height: 12),
              _buildMenuButton(
                onPressed: () {
                  FlameAudio.bgm.resume();
                  // 回到首页
                  if (myGame != null) {
                    myGame.onGameFinished();
                  }
                  game.overlays.remove('pause');
                },
                text: '回到首页',
              ),
              const SizedBox(height: 16),
            ],
          ),
        ),
      ),
    );
  }
}